package com.tang.client;

import com.tang.future.SyncWriteFuture;
import com.tang.future.SyncWriteMap;
import com.tang.msg.Response;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * client配置
 *
 * @author fulin.tang
 * @version V3.0
 * @since 2022/5/19 12:19
 */
public class MyClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        // 将msg转换成我们自己的实体类
        Response response = (Response) msg;
        String requestId = response.getRequestId();
        SyncWriteFuture future = (SyncWriteFuture) SyncWriteMap.getInstance().syncKey.get(requestId);
        if (future != null) {
            future.setResponse(response);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }
    
}
